var express = require('express');
var router = express.Router();
const db = require('./db');


/* GET home page. */
router.get('/', function (req, res, next) {
  res.render('index', {
    title: 'Express'
  });

});
//查询接口
router.all('/shaveAll', function (req, res, next) {
  let odata;
  if (Object.keys(req.query).length) {
    odata = req.query;
  } else {
    odata = req.body;
  }
  let sql = `SELECT * FROM user`
  db(sql, (err, data) => {
    if (err) {
      throw err
    }

    res.send(data)
  })
})
router.all('/zhanshi', function (req, res, next) {
  let odata;
  if (Object.keys(req.query).length) {
    odata = req.query;
  } else {
    odata = req.body;
  }
  let sql = `SELECT * FROM zhanshi`
  db(sql, (err, data) => {
    if (err) {
      throw err
    }
    res.send(data)
  })
})
router.all('/news', function (req, res, next) {
  let odata;
  if (Object.keys(req.query).length) {
    odata = req.query;
  } else {
    odata = req.body;
  }
  let sql = `SELECT * FROM news`
  db(sql, (err, data) => {
    if (err) {
      throw err
    }
    res.send(data)
  })
})
router.all('/jiameng', function (req, res, next) {
  let odata;
  if (Object.keys(req.query).length) {
    odata = req.query;
  } else {
    odata = req.body;
  }
  let sql = `SELECT * FROM jiameng`
  db(sql, (err, data) => {
    if (err) {
      throw err
    }
    res.send(data)
  })
})

//注册接口
router.all('/zhuce', function (req, res, next) {
  let odata;
  if (Object.keys(req.query).length) {
    odata = req.query;
  } else {
    odata = req.body;
  }
  let {
    name,
    number,
    password
  } = odata
  let sql = ` insert into user value (null, '${name}','${number}','${password}');`
  db(sql, (err, data) => {
    if (err) {
      throw err
    }
    res.send("注册成功")
  })

})
//登录接口
router.all('/denglu', function (req, res, next) {
  let odata;
  if (Object.keys(req.query).length) {
    odata = req.query;
  } else {
    odata = req.body;
  }
  let {
    name,
    number,
    password
  } = odata
  let sql = `select * from user where name = "${name}"`
  db(sql, (err, data) => {
    if (err) {
      throw err
    }
    res.send(JSON.stringify(data))
  })
})
//删除接口
router.all('/shanchu', function (req, res, next) {
  let odata;
  if (Object.keys(req.query).length) {
    odata = req.query;
  } else {
    odata = req.body;
  }
  let {
    id
  } = odata
  console.log(odata);
  let sql = `DELETE FROM user WHERE id = ${id}`
  db(sql, (err, data) => {
    if (err) {
      throw err
    }
    res.send(data)
  })
})
module.exports = router;